const mutations = {

      //初始化weosocket
	    initWebSocket(state,deviceid){
	      if(typeof(WebSocket) === "undefined"){
	        alert("您的浏览器不支持WebSocket")
	        return false
	      }

        console.log(deviceid);
        var ip = window.document.domain;
        console.log('ip = ' + ip);
	      //const wsuri = 'ws://192.168.2.64:8093/deviceWebSocket/' + username // websocket地址
        const wsuri = 'ws://'+ip+':8080/deviceWebSocket/' + deviceid;
        console.log('wensocket请求地址 = '+ wsuri);
	      this.websock = new WebSocket(wsuri);
	      this.websock.onopen = this.websocketonopen;
	     // this.websock.onmessage = this.websocketonmessage;
        //接收后端返回的数据
        this.websock.onmessage = function(e){
          // this.dataJson = JSON.parse(e.data);
          // console.log(this.dataJson);
          // localStorage.setItem('dataJson',e.data);
          state.imMessages=JSON.parse(e.data);
          console.log('state中数据已更新');
          window.localStorage.setItem('dataJson',JSON.stringify(state.imMessages));
          //let state = dataJson.jsondata[state];
          //console.log(state);
          // 在这里使用后端返回的数据，对数据进行处理渲染
        };

	      this.websock.onerror = this.websocketonerror;
	      this.websock.onclose = this.websocketclose;
	    },

      //连接成功
	    websocketonopen(){
	      console.log('WebSocket连接成功');
        // let actions = {你要发送给后台的参数};     //请根据实际项目需要进行修改
        // this.websocketsend(JSON.stringify(actions));
	    },

      // websocketsend(Data){//数据发送
      //   this.websock.send(Data);
      // },

      //接收后端返回的数据
	    // websocketonmessage(e){
	    //   // this.dataJson = JSON.parse(e.data);
      //   // console.log(this.dataJson);
      //   // localStorage.setItem('dataJson',e.data);
      //   state.imMessages=JSON.parse(e.data);
      //   console.log('state中数据已更新');
      //   window.localStorage.setItem('dataJson',JSON.stringify(state.imMessages));
      //   //let state = dataJson.jsondata[state];
      //   //console.log(state);
	    //   // 在这里使用后端返回的数据，对数据进行处理渲染
	    // },

      //连接建立失败重连
	    websocketonerror(e){
	      console.log('连接失败的信息：', e);
	      this.initWebSocket(); // 连接失败后尝试重新连接
	    },

       //关闭连接
	    websocketclose(e){
	      console.log('断开连接',e);
	    },

  //websocket初始化
  // initWebsocket(state,itemId,userId){
  //     const wsUrl='websocket链接地址';
  //     const ws=new WebSocket(wsUrl);
  //     //建立连接
  //     ws.onopen=function () {
  //         let communicationMessage=new Socket.CommunicationMessage(null,itemId, userId, null, null);
  //         let dataContent=new Socket.DataContent(Socket.MsgActionEnum.CONNECT,communicationMessage,null);
  //         //调用send向服务器发送数据
  //         if(ws.readyState===1){
  //             ws.send(JSON.stringify(dataContent));
  //         }
  //     };
  //     //接收服务端消息
  //     ws.onmessage=function (e) {
  //         state.imMessages=JSON.parse(e.data);
  //         console.log('state中数据已更新');
  //         window.localStorage.setItem('imMsg',JSON.stringify(state.imMessages));
  //     };
  //     ws.close=function () {
  //         console.log('连接已断开');
  //     }
  // },
};
export default mutations;